---
import { type CollectionEntry, getCollection } from "astro:content";
import PostDetails from "@/layouts/PostDetails.astro";
import getSortedPosts from "@/utils/getSortedPosts";
import { getSlugFromPath } from "@/utils/getPath";

export interface Props {
  post: CollectionEntry<"blog">;
}

export async function getStaticPaths() {
  // 开发环境下包含草稿文章，生产环境下排除草稿文章
  const posts = await getCollection("blog", ({ data }) => {
    if (import.meta.env.DEV) {
      return true; // 开发环境显示所有文章包括草稿
    }
    return !data.draft; // 生产环境排除草稿
  });
  const postResult = posts.map(post => ({
    params: { slug: getSlugFromPath(post.id, post.filePath) },
    props: { post },
  }));

  return postResult;
}

const { post } = Astro.props;

const posts = await getCollection("blog");
const sortedPosts = getSortedPosts(posts);
---

<PostDetails post={post} posts={sortedPosts} />